package com.house.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoginInterceptor implements HandlerInterceptor {
	//在handler执行之前执行此方法
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		
		//请求ur
		
		String url = request.getRequestURI();
		if(url.indexOf("login")>=0||url.indexOf("choose")>=0||url.indexOf("index")>=0){
		if(url.indexOf("login") >= 0){
			//如果是登入提交则，放行
			return true;
		}
		HttpSession session = request.getSession();
		String userId = (String)session.getAttribute("userId");
		String adminId = (String)session.getAttribute("adminId");
		if (url.indexOf("choose") >= 0) {
			if(userId !=null) {
				return true;
				}
			request.getRequestDispatcher("/WEB-INF/jsp/ulogin.jsp").forward(request, response);
			return true;
			
			
		}else  if(adminId !=null) {
			return true;
		}

		
		
		//跳转登录页
		request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
		}
		return true;
	}
	//在handler返回modelandview前执行此方法
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub

	}
	//进入handler方法之后执行
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub

	}

}
